jsx3.Class.defineClass(
        "tibco.admin.monitormanage.uicomponents.table.machines.Machines", //the full name of the class to create
        jsx3.gui.Block, //  extends
        null, // implements
        function (plugin) {
        	plugin.machineCacheId = "SPM_MACHINES_TABLE_CACHE";
            plugin.dashboardScreenId = "tibco.admin.monitormanage.uicomponents.page.dashboard.Dashboard.dashboardScreen";
        	plugin.wsEndpointURL = tibco.admin.monitormanage.framework.mainPage.MainPage.wsEndpointURL;
        	
        	plugin.prototype.listenForEnvironment;
        	plugin.prototype.screenId;
			plugin.prototype.nameSuffix = "";
        	plugin.prototype.allowAllEnvironments;
        
            plugin.prototype.init = function() {
                this.jsxsuper();
            }
            
            plugin.prototype.getNamespace = function() {
				return "tibco.admin";
			}
			
			plugin.prototype.setUpComponent = function(nameSuffix, listenForEnvironment, allowAllEnvironments, screenId, loadDataImmediately) {
				var server = tibco.admin.Main.getServer();
				server.Cache.clearById(plugin.machineCacheId);
			
				this.screenId = screenId;
				this.nameSuffix = nameSuffix;
				var tableLabel = this.getDescendantOfName("machinesTableLabel");
				tableLabel.setName(tableLabel.getName() + nameSuffix);
				var machinesHelpButton = this.getDescendantOfName("machinesHelpButton");
				machinesHelpButton.setName(machinesHelpButton.getName() + nameSuffix);
				var machinesTable = this.getDescendantOfName("machinesTable");
				machinesTable.setName(machinesTable.getName() + nameSuffix);
				var graphCol = machinesTable.getDescendantOfName("memoryCol");
				var backgroundImg = server.resolveURI('jsxaddin://user!MatrixAdmin/images/matrix/progressBarBkgrdTile.jpg');
				var redFill = server.resolveURI('jsxaddin://user!MatrixAdmin/images/matrix/progressBarRedFill.jpg');
				var greenFill = server.resolveURI('jsxaddin://user!MatrixAdmin/images/matrix/progressBarGreenFill.jpg');
				var yellowFill = server.resolveURI('jsxaddin://user!MatrixAdmin/images/matrix/progressBarYellowFill.jpg');
				var valueTemplate = graphCol.getValueTemplate();
				valueTemplate = valueTemplate.replace("___BACKGROUND___", backgroundImg);
				valueTemplate = valueTemplate.replace("___REDFILL___", redFill);
				valueTemplate = valueTemplate.replace("___GREENFILL___", greenFill);
				valueTemplate = valueTemplate.replace("___YELLOWFILL___", yellowFill);
				graphCol.setValueTemplate(valueTemplate);
				
	            if(listenForEnvironment)
	            { tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.environmentChangeSubject, this.environmentSelected, this); }
	            tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.general.NavigationHandler.mainPageDisplayTopic, this.handleDisplayScreen, this);
				this.listenForEnvironment = listenForEnvironment;
				this.allowAllEnvironments = allowAllEnvironments;
				if(loadDataImmediately)
				{ this.callGetMachines(listenForEnvironment); }
                 if(this.screenId==plugin.dashboardScreenId)
                { this.getDescendantOfName("machinesHelpButton" + this.nameSuffix).setDisplay(jsx3.gui.Block.DISPLAYNONE, true); }
            }
            
        	plugin.showMachineDetail = function(machineIdVal) {
        		var paramArray = { machineId:machineIdVal };
        		tibco.admin.monitormanage.general.NavigationHandler.publishMainPageNavigationMessage(tibco.admin.monitormanage.uicomponents.page.gridInfrastructure.GridInfrastructure.machineScreenId, paramArray); 
        	}
        	
            plugin.prototype.environmentSelected = function(subject, message, tableObj) {
            	var envId = message.environmentId;
            	tableObj.callGetMachines(tableObj.listenForEnvironment, envId);
            }
            
            plugin.prototype.updateComponent = function() {
            	var makeWSCall = false;
            	if(!this.listenForEnvironment)
            	{ makeWSCall = true; }
            	else
            	{
            	 	if(this.allowAllEnvironments)
            	 	{ makeWSCall = true; }
            	 	else
            	 	{
            	 		var environmentId = tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.getCurrentEnvironmentId();
            	 		if(environmentId && environmentId != "" && environmentId != tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.allEnvironmentsId)
            	 		{ makeWSCall = true; }
            	 	}
            	}
            	if(makeWSCall)
				{ this.callGetMachines(this.listenForEnvironment, null); }
			}
			
			plugin.prototype.handleDisplayScreen = function(subject, message, tableObj) {
            	if(message.screenId == tableObj.screenId)
            	{
            		var environmentId = null; 
            		var makeWSCall = false;
            		if(!tableObj.listenForEnvironment)
            		{ makeWSCall = true; }
            		else
            		{
            	 		if(this.allowAllEnvironments)
            	 		{ makeWSCall = true; }
            	 		else
            	 		{
            	 			environmentId = message.environmentId;
            	 			if(!environmentId)
            	 			{ environmentId = tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.getCurrentEnvironmentId(); }
            	 			if(environmentId && environmentId != "" && environmentId != tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.allEnvironmentsId)
            	 			{ makeWSCall = true; }
            	 		}
            		}	
            		if(makeWSCall)
					{ tableObj.callGetMachines(tableObj.listenForEnvironment, environmentId); } 
            	}
            }
        	
        	//call this method to begin the service call (eg.service.callgetMachines();)
    		plugin.prototype.callGetMachines = function(listenForEnvironment, environmentId) {
    			if(tibco.admin.Main.getServer().getEnv("MODE")) 
    			{
					// mode is live
					var objService = tibco.admin.Main.loadService(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/table/machines/rules/machinesTableRules.xml", "getMachines");
					objService.setEndpointURL(plugin.wsEndpointURL);
					// this setInboundURL should not be required...only here because of a current AMX framework bug
					objService.setInboundURL("tibco/admin/monitormanage/uicomponents/table/machines/data/machinesTableData.xml");
				}
				else 
				{
					var objService = new jsx3.net.Service(tibco.admin.Main.resolveURI(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/table/machines/rules/machinesTableRules.xml"), "getMachines", null, null);
					// mode is static
					objService.setInboundURL(tibco.admin.Main.resolveURI(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/table/machines/data/machinesTableData.xml"));
				}
				
				objService.machineObj = this;

				if(listenForEnvironment)
				{
					if(!environmentId)
					{ environmentId = tibco.admin.monitormanage.uicomponents.dropdown.environment.Environment.getCurrentEnvironmentId(); }
					objService.environmentId = environmentId;
				}
				
				// initialize the list of metrics we wish to retrieve data for
				var metricsList = new jsx3.util.List();
				metricsList.add("OS");
				metricsList.add("totalMemory");
				metricsList.add("memoryInUse");
				objService.metricsList = metricsList;

      			//subscribe and call
      			objService.subscribe(jsx3.net.Service.ON_SUCCESS, this.ongetMachinesSuccess);
      			objService.subscribe(jsx3.net.Service.ON_ERROR, this.ongetMachinesError);
      			objService.subscribe(jsx3.net.Service.ON_INVALID, this.ongetMachinesInvalid);
      			objService.doCall();
    		}

    		plugin.prototype.ongetMachinesSuccess = function(objEvent) {
      			var machineObj = objEvent.target.machineObj;
				var tableObj = machineObj.getDescendantOfName("machinesTable" + machineObj.nameSuffix);
				tableObj.repaintData();
    		}

    		plugin.prototype.ongetMachinesError = function(objEvent) {
      			var myStatus = objEvent.target.getRequest().getStatus();
      			jsx3.log("Error","The service call failed. The HTTP Status code is: " + myStatus);
    		}

    		plugin.prototype.ongetMachinesInvalid = function(objEvent) {
    			jsx3.log("Invalid","The following message node just failed validation:\n\n" + objEvent.message);
   			}
   			
        });